% function [conspicuity_map, pyrcurr] = conspicuity_hw_sim(img, pars, pyrprev)
function [conspicuity_map] = conspicuity_hw_sim(img_prev,img_curr, pars)
%% input parameters
img_size_x = pars.img_size_x;
img_size_y = pars.img_size_y;
Nc = pars.Nc;
Ns = pars.Ns;
delta = pars.delta;
c = pars.c;
pyr_filt_coeff = pars.pyr_filt_coeff;
resample_filt_coeff = pars.resample_filt_coeff;
levels = pars.levels;
out_scale = pars.outscale;
dec_factor = pars.dec_factor;
theta = pars.theta;
pyr_select = pars.pyr_select; % 0 : gaussian pyramid, 1 : laplacian pyramid
gabor_enable = pars.gabor_enable; % 0 : gabor filter disable, 1 : gabor filter enable
reichardt_enable = pars.reichardt_enable; %1 to enable reichardt
frame_ref=pars.frame_ref;				% for first frame do not do anything ie for frame_ref=1
flicker_enable=pars.flicker_enable;

output_nr = img_size_y/(dec_factor^out_scale);  %size of output conspicuity map
output_nc = img_size_x/(dec_factor^out_scale);

cs_ch = zeros(Nc*Ns,2);
orientation_ch = zeros(output_nr,output_nc);

orientation_ch_acc = zeros(output_nr,output_nc);
% the center-surround levels for each channel
for i = 1:Nc
    clev = c(i);
    for j = 1:Ns
        slev = clev + delta(j);
        k = (i-1)*Ns+j;
        cs_ch(k,1) = clev;
        cs_ch(k,2) = slev;
    end
end

% t_index = 1;
%% computation begins
% for t_index = 1:N_theta   %loop over theta
%persistent pyrprev;
    
    
    pyr = pyramid_sim(img_curr, pyr_filt_coeff, levels, pyr_select);         %Laplacian Pyramid generation
%gabor
    if(gabor_enable == 1)
         for pyrlev = 1:levels+1            %generate gabor pyramid
            pyr_gabor{pyrlev} = gabor_sim(pyr{pyrlev}, theta, pars);
         end
    end
%reichardt	
	if(reichardt_enable==1)
		if(frame_ref>1)
		
			pyrcurr = pyramid_sim(img_curr, pyr_filt_coeff, levels, pyr_select); 
            pyrprev = pyramid_sim(img_prev, pyr_filt_coeff, levels, pyr_select); 
			for pyrlev = 1:levels+1            
			pyr_reichardt{pyrlev} = independent_reichardt_sim(pyrprev{pyrlev},pyrcurr{pyrlev}, pars);
            end
		end
    end
    
%flicker
    if(flicker_enable==1)
        if(frame_ref>1)
            flick_img= abs(img_curr-img_prev);
            pyr_flicker = pyramid_sim(flick_img, pyr_filt_coeff, levels, pyr_select);
        end
    end
    
%for all channels
    for lpi = 1:Nc*Ns   

        clev = cs_ch(lpi,1); %center channel for csd
        slev = cs_ch(lpi,2); %surround channel for csd
        cs_factor = 2^(slev-clev);

        
%         %gabor/reichardt/flickr select
%         if(gabor_enable == 1)
%             center = pyr_gabor{clev+1};   %+1 to for matlab array index
%             surround = pyr_gabor{slev+1};
%         elseif((reichardt_enable == 1) && (frame_ref ~=1))
%             center = pyr_reichardt{clev+1};   %+1 to for matlab array index
%             surround = pyr_reichardt{slev+1};        
%         elseif((flicker_enable == 1) && (frame_ref ~=1))
%             center = pyr_flicker{clev+1};   %+1 to for matlab array index
%             surround = pyr_flicker{slev+1};        
%         else
%             center = pyr{clev+1};   %+1 to for matlab array index
%             surround = pyr{slev+1};
%         end
		
%         %reichardt
% 		if((reichardt_enable == 1) && (frame_ref ~=1))
% 		center = pyr_reichardt{clev+1};   %+1 to for matlab array index
%         surround = pyr_reichardt{slev+1};
%         end
%         
%         %flicker
%         if((flicker_enable == 1) && (frame_ref ~=1))
% 		center = pyr_flicker{clev+1};   %+1 to for matlab array index
%         surround = pyr_flicker{slev+1};
%         end
        
        %gabor/reichardt/flickr select
        if(gabor_enable == 1)
            center = pyr_gabor{clev+1};   %+1 to for matlab array index
            surround = pyr_gabor{slev+1};
        elseif((reichardt_enable == 1) && (frame_ref ~=1))
            center = pyr_reichardt{clev+1};   %+1 to for matlab array index
            surround = pyr_reichardt{slev+1};
        elseif((flicker_enable == 1) && (frame_ref ~=1))
            center = pyr_flicker{clev+1};   %+1 to for matlab array index
            surround = pyr_flicker{slev+1};
        else
            center = pyr{clev+1};   %+1 to for matlab array index
            surround = pyr{slev+1};
        end
		
       csd_out = csd_sim(center, surround, cs_factor);     %Center-surround difference
     
       norm_out = maxnorm_sim(csd_out);                    % Max normalizer
       
       maxn = max(max(norm_out));
%        disp(sprintf('frame %d stats - c-s level %d - range after max norm = %E', frame_ref, lpi, maxn));
       
        resample_lev = abs(out_scale-clev);  % num of levels to output scale
        resample_out = resample_sim(norm_out, resample_filt_coeff, resample_lev); %Resample to output scale

        orientation_ch = orientation_ch + resample_out;    %Across-scale addition
    end
    
%     if((reichardt_enable==1) & (frame_ref == 1))
%         conspicuity_map=zeros(output_nr,output_nc);
%     else
        conspicuity_map = maxnorm_sim(orientation_ch);  
%     end
    
% 	if((((reichardt_enable==1) || (flicker_enable==1))) && (frame_ref>1))
%         conspicuity_map = maxnorm_sim(orientation_ch);      %Final normalization to get the consicuity map
% 	else 
% 	    conspicuity_map=zeros(output_nr,output_nc);
%     end
%     
%      if((reichardt_enable==0) && (flicker_enable==0))   
%      conspicuity_map = maxnorm_sim(orientation_ch);  
%      end
% 	
   
% end






